package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.api.SearchProductService;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.service.ITestProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @auther cskaoyan
 * @date 2022/5/18:11:34
 */
@RestController
public class TestProductDetailController implements SearchProductService {

    @Autowired
    ITestProductService  testProductService;
    @Autowired
    IProductService iProductService;



    @GetMapping("/shopping/test")
    public ResponseData getProductDetail(Long productId) {

//        TestProductDetailRequest request = new TestProductDetailRequest();
//        request.setProductId(productId);
//
//
//        // 根据业务逻辑处理请求
  //  TestProductDetailResponse productDetail = testProductService.getProductDetail(request);
//
//        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
//            // 业务方法调用成功
//            TestProductDetailDto result = productDetail.getTestProductDetailDto();
//            return new ResponseUtil().setData(result);
//        }

     //    业务方法执行失败
      return null;//new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }

    @GetMapping("/shopping/product/{id}")
    public ResponseData getProduct(@PathVariable("id")Long id) {

        ProductDetailRequest request = new ProductDetailRequest();
       request.setId(id);
        // 根据业务逻辑处理请求
        ProductDetailResponse productDetail = iProductService.getProductDetail(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
            // 业务方法调用成功
            ProductDetailDto productDetailDto = productDetail.getProductDetailDto();
            return new ResponseUtil().setData(productDetailDto);
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());

    }
    @GetMapping("/shopping/recommend")

    public ResponseData recommend() {

        // 根据业务逻辑处理请求
        RecommendResponse recommendGoods = iProductService.getRecommendGoods();

        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) {
            // 业务方法调用成功
            Set<PanelDto> panelContentItemDtos = recommendGoods.getPanelContentItemDtos();
            return new ResponseUtil().setData(panelContentItemDtos);
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());

    }

    @GetMapping("/shopping/goods")
    public ResponseData goods(   AllProductRequest request ) {
//        AllProductRequest allProductRequest = new AllProductRequest();

        // 根据业务逻辑处理请求
        AllProductResponse allProduct = iProductService.getAllProduct(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            // 业务方法调用成功
//            List<ProductDto> productDtoList = allProduct.getProductDtoList();

            PageResponse pageResponse = new PageResponse(allProduct.getProductDtoList(), allProduct.getTotal());
            return new ResponseUtil().setData(pageResponse);
        }
        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(allProduct.getMsg());

    }


    @Override
    @GetMapping("/rpc/items")
    public AllItemResponse getAllProductItem() {
        return iProductService.getAllItems();
    }
}
